package xps.and.uudaijia.userclient.view.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.jude.utils.JUtils;

import java.util.List;

import rx.Subscriber;
import xps.and.uudaijia.userclient.adapter.OrderAdapterDaijia;
import xps.and.uudaijia.userclient.data.baen.CodeBean;
import xps.and.uudaijia.userclient.data.baen.OrderListRes;
import xps.and.uudaijia.userclient.data.net.UserNetWorks;
import xps.and.uudaijia.userclient.util.ListDividerItemDecoration;
import xps.and.uudaijia.userclient.view.activity.CommentDriverActivity;
import xps.and.uudaijia.userclient.view.activity.MapActivity;
import xps.and.uudaijia.userclient.view.activity.PaymentActivity;
import xps.and.uudaijia.userclient.view.base.BaseSwipeRefreshFragment;

public class OrderDaijiaFragment extends BaseSwipeRefreshFragment {
    String userId;

    @Override
    protected void getData(int pageIndex) {
        int page = pageIndex + 1;
        doLoadMore(page);
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        int page = pageIndex + 1;
        doLoadMore(page);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        ListDividerItemDecoration list = new ListDividerItemDecoration((int) (1 * getActivity().getResources().getDisplayMetrics().density));
        recyclerView.addItemDecoration(list);
        setRefreshEnable(false);
        abstractRecyclerAdapter = new OrderAdapterDaijia(activity, recyclerView);
        abstractRecyclerAdapter.setAutoLoadMoreEnable(true);
        abstractRecyclerAdapter.setOnItemClickListener((v, p) -> {
            OrderListRes.ReturnBodyBean dataBean = (OrderListRes.ReturnBodyBean) abstractRecyclerAdapter.getItem(p);
            if (Integer.valueOf(dataBean.getOrderStatus()) == 0) {//状态5为待支付订
                toMainAc(dataBean);
            } else if (Integer.valueOf(dataBean.getOrderStatus()) == 1) {
                toMainAc(dataBean);
            } else if (Integer.valueOf(dataBean.getOrderStatus()) == 2) {
                toMainAc(dataBean);
            } else if (Integer.valueOf(dataBean.getOrderStatus()) == 3) {
                toMainAc(dataBean);
            } else if (Integer.valueOf(dataBean.getOrderStatus()) == 4) {
                toMainAc(dataBean);
            } else if (Integer.valueOf(dataBean.getOrderStatus()) == -1) {
                toDelete(dataBean);
            } else {
                if (Integer.valueOf(dataBean.getPayStatus()) == 1) {
                    if (Integer.valueOf(dataBean.getIsAppraises()) == 1) {
                        JUtils.Toast("该订单已评论");
                    } else {
                        toCommetnAc(dataBean);
                    }
                } else {
                    toPaymentAc(dataBean);
                }
            }
        });
        douserId();
        super.onActivityCreated(savedInstanceState);
    }

    void douserId() {
        userId = JUtils.getSharedPreference().getString("User_ID", "");
        UserNetWorks.getOrderList("1", "10", "1", new Subscriber<OrderListRes>() {
            @Override
            public void onCompleted() {
                Log.e("getOrderList", "onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(OrderListRes orderListRes) {
                List<OrderListRes.ReturnBodyBean> list = orderListRes.getReturn_body();

                if (list != null && list.size() <= 0) {
                    onFail("没有更多");
                    return;
                }
                adapterListData = list;
                onResponce();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        douserId();
    }

    public void doLoadMore(final int pageIndex) {
        //onRefresh();

        Log.e("doLoadMore", pageIndex + "");
        userId = JUtils.getSharedPreference().getString("User_ID", "");
        UserNetWorks.getOrderList("1", "10", pageIndex + "", new Subscriber<OrderListRes>() {
            @Override
            public void onCompleted() {
                Log.e("getOrderList", "onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(OrderListRes orderListRes) {
                List<OrderListRes.ReturnBodyBean> list = orderListRes.getReturn_body();
                Log.e("onNext", list.toString());

                if (list != null && list.size() == 0) {
                    onFail("没有更多");
                    return;
                }
                if (pageIndex == 1) {
                    adapterListData = list;
                } else {

                    adapterListData.clear();
                    abstractRecyclerAdapter.notifyDataSetChanged();
                    adapterListData.addAll(list);
                }
                onResponce();
            }
        });
    }

    public void toMainAc(OrderListRes.ReturnBodyBean dataBean) {
        Intent intent = new Intent(getActivity(), MapActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("OrderId", dataBean.getOrderId());
        intent.putExtra("beginLng", dataBean.getBeginLng());
        intent.putExtra("beginAddress", dataBean.getBeginAddress());
        intent.putExtra("endLng", dataBean.getEndLng());
        intent.putExtra("endLat", dataBean.getEndLat());
        intent.putExtra("endAddress", dataBean.getEndAddress());
        intent.putExtra("userPhone", dataBean.getDriverPhone());
        intent.putExtra("remark", dataBean.getRemark());
        intent.putExtra("beginLat", dataBean.getBeginLat());
        intent.putExtra("orderStatus", dataBean.getOrderStatus());
        intent.putExtra("driverName", dataBean.getDriverName());
        intent.putExtra("driverHeadImg", dataBean.getDriverHeadImg());
        intent.putExtra("orderType", "1");
        intent.putExtra("departureTime", dataBean.getDepartureTime());
        getActivity().startActivity(intent);
    }

    public void toPaymentAc(OrderListRes.ReturnBodyBean dataBean) {
        Intent intent = new Intent(getActivity(), PaymentActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("OrderId", dataBean.getOrderId());
        intent.putExtra("driverName", dataBean.getDriverName());
        intent.putExtra("driverHeadImg", dataBean.getDriverHeadImg());
        intent.putExtra("driverPhone", dataBean.getDriverPhone());
        intent.putExtra("liebia", "2");
        getActivity().startActivity(intent);
    }

    public void toCommetnAc(OrderListRes.ReturnBodyBean dataBean) {
        Intent intent = new Intent(getActivity(), CommentDriverActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("orderId", dataBean.getOrderId());
        intent.putExtra("driverName", dataBean.getDriverName());
        intent.putExtra("driverHeadImg", dataBean.getDriverHeadImg());
        getActivity().startActivity(intent);
    }

    public void toDelete(OrderListRes.ReturnBodyBean dataBean) {
        final Bundle bundle = getArguments();
        final AlertDialog.Builder normalDialog = new AlertDialog.Builder(getContext());
        normalDialog.setTitle("提示");
        normalDialog.setMessage("是否删除该订单");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        UserNetWorks.getDeldet(dataBean.getOrderId(), new Subscriber<CodeBean>() {
                            @Override
                            public void onCompleted() {

                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onNext(CodeBean codeBean) {
                                if (codeBean.getReturn_code().equals("SUCCESS")) {
                                    JUtils.Toast(codeBean.getReturn_msg());
                                    adapterListData.remove(dataBean);
                                    abstractRecyclerAdapter.notifyDataSetChanged();
                                    douserId();
                                } else if (codeBean.getReturn_code().equals("FAIL")) {
                                    JUtils.Toast(codeBean.getReturn_msg());
                                }

                            }
                        });
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        normalDialog.show();
    }


}
